package com.vay.de.dang.online.ui.apply;

import android.os.Bundle;
import android.view.View;

import com.vay.de.dang.online.resp.RCollectionAccountDto;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.view.DialogMenuOption;
import com.vay.de.dang.online.web.LoanApi;
import com.vay.de.dang.online.web.ReqResult;
import com.vay.de.dang.online.R;
import com.vay.de.dang.online.entiy.ModifyAccountEntity;
import com.vay.de.dang.online.entiy.MenuItemEntity;
import com.vay.de.dang.online.util.RequestHelper;
import com.vay.de.dang.online.util.ResponseHelper;
import com.vay.de.dang.online.databinding.ActivityModifyAccountBinding;
import com.vay.de.dang.online.resp.RCodeTableDto;
import com.vay.de.dang.online.web.TableApi;
import com.vay.de.dang.online.web.ReqCallback;
import com.vay.de.dang.online.ui.WithBack1Activity;

public class AccountModifyActivity extends WithBack1Activity {

    private ActivityModifyAccountBinding binding;
    private ModifyAccountEntity accountEntity = new ModifyAccountEntity();
    private MenuItemEntity withdrawalMethods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityModifyAccountBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        findViewById(R.id.bnt_back_home).setOnClickListener(view -> finish());
        findViewById(R.id.bnt_pre).setOnClickListener(view -> finish());
        findViewById(R.id.bnt_next).setOnClickListener(view -> {

            // 收款方式
            if (!VnUtil.isDropList(binding.withdrawalMethods, "Not empty")) return;
            if (withdrawalMethods == null) {
                showHint("Please Choice withdrawal method");
                return;
            }

            // 电子钱包
            if ("2".equals(withdrawalMethods.getValue())) {
                if (!VnUtil.isDropList(binding.eWallet, "Not empty")) return;
                if (!VnUtil.isRuleOk(binding.account,
                        RequestHelper.findBaseRule("bankCardNumber"))) return;
                accountEntity.setBankCardNumber(binding.account.getContent());
            }
            // 线下取款
            else if ("3".equals(withdrawalMethods.getValue())) {
                if (!VnUtil.isDropList(binding.shopName, "Not empty")) return;
                accountEntity.setBankCardNumber(null);
            }
            // 银行
            else {
                if (!VnUtil.isDropList(binding.bank, "Not empty")) return;
                if (!VnUtil.isRuleOk(binding.account,
                        RequestHelper.findBaseRule("bankCardNumber"))) return;
                accountEntity.setBankCardNumber(binding.account.getContent());
            }

            // firstName
            if (!VnUtil.isRuleOk(binding.name,
                    RequestHelper.findBaseRule("firstName"))) return;
            accountEntity.setFirstName(binding.name.getContent());

            // 修改
            LoanApi.modifyCollectionAccount(accountEntity, new ReqCallback(mActivity) {
                @Override
                public void callback(ReqResult value) {
                    if (value.isOk()) {
                        setResult(RESULT_OK);
                        finish();
                    }
                }
            }.showDialog(true));
        });

        RCollectionAccountDto dto = (RCollectionAccountDto) getIntent().getSerializableExtra("obj");
        loadView();
        loadData(dto);
    }

    private void loadView() {
        // 收款方式
        binding.withdrawalMethods.setOnItemClick(() -> {
            new DialogMenuOption(mActivity)
                    .setOnOptionItemClick(bean -> {
                        binding.withdrawalMethods.setContent(bean.getLabel());
                        accountEntity.setWithdrawalType(VnUtil.toIntValue(bean.getValue()));
                        withdrawalMethods = bean;
                        controlView(bean);
                    })
                    .showChoice(getString(R.string.label_withdrawal_method), TableApi.WITHDRAWAL_TYPE);
        });

        // 银行
        binding.bank.setOnItemClick(() -> {
            withdrawalMethods(1);
        });

        // 电子钱包
        binding.eWallet.setOnItemClick(() -> {
            withdrawalMethods(2);
        });

        // 门店
        binding.shopName.setOnItemClick(() -> {
            withdrawalMethods(3);
        });
    }

    private void controlView(MenuItemEntity bean) {
        // 银行
        if ("1".equals(bean.getValue())) {
            binding.bank.setVisibility(View.VISIBLE);
            binding.eWallet.setVisibility(View.GONE);
            binding.shopName.setVisibility(View.GONE);
            binding.account.setVisibility(View.VISIBLE);
        }
        // 电子钱包
        else if ("2".equals(bean.getValue())) {
            binding.bank.setVisibility(View.GONE);
            binding.eWallet.setVisibility(View.VISIBLE);
            binding.shopName.setVisibility(View.GONE);
            binding.account.setVisibility(View.VISIBLE);
        }
        // 线下取款
        else if ("3".equals(bean.getValue())) {
            binding.bank.setVisibility(View.GONE);
            binding.eWallet.setVisibility(View.GONE);
            binding.shopName.setVisibility(View.VISIBLE);
            binding.account.setVisibility(View.GONE);
        }
    }

    private void withdrawalMethods(int value) {
        if (withdrawalMethods != null) {
            String title = "Bank";
            String typeCode;
            if ("2".equals(withdrawalMethods.getValue())) {
                typeCode = TableApi.ELECTRONIC_WALLET;
                title = "eWallet";
            } else if ("3".equals(withdrawalMethods.getValue())) {
                typeCode = TableApi.STORE_NAME;
                title = "ShopName";
            } else {
                typeCode = TableApi.WITHDRAWAL_METHOD;
            }
            new DialogMenuOption(mActivity)
                    .setOnOptionItemClick(bean -> {
                        accountEntity.setWithdrawalMethod(bean.getValue());
                        if (value == 1) {
                            binding.bank.setContent(bean.getLabel());
                        } else if (value == 2) {
                            binding.eWallet.setContent(bean.getLabel());
                        } else if (value == 3) {
                            binding.shopName.setContent(bean.getLabel());
                        }
                    })
                    .showChoice(title, typeCode);
        }
    }

    private void loadData(RCollectionAccountDto dto) {
        // 收款类型
        RCodeTableDto tableDto = dto.getWithdrawalType();
        if (tableDto != null) {
            binding.withdrawalMethods.setContent(tableDto.getLabel());
            accountEntity.setWithdrawalType(VnUtil.toIntValue(tableDto.getValue()));
            withdrawalMethods = ResponseHelper.toOptionItem(tableDto);
            String value = withdrawalMethods.getValue();

            // 收款类型
            tableDto = dto.getWithdrawalMethod();
            if (tableDto != null) {
                controlView(withdrawalMethods);
                // 银行转账
                if ("1".equals(value)) {
                    binding.bank.setContent(tableDto.getLabel());
                    binding.account.setContent(dto.getBankCardNumber());
                    accountEntity.setWithdrawalMethod(tableDto.getValue());
                }
                // 电子钱包
                else if ("2".equals(value)) {
                    binding.eWallet.setContent(tableDto.getLabel());
                    binding.account.setContent(dto.getBankCardNumber());
                    accountEntity.setWithdrawalMethod(tableDto.getValue());
                }
                // 线下取款
                else if ("3".equals(value)) {
                    binding.shopName.setContent(tableDto.getLabel());
                    accountEntity.setWithdrawalMethod(null);
                }
            }
        }

        // name
        binding.name.setContent(dto.getBankUserName());
    }
}
